//
//  UserShareListCell.m
//  HappyBaby
//
//  Created by 华哥 on 15/11/28.
//  Copyright © 2015年 华哥. All rights reserved.
//

#import "UserShareListCell.h"
#import "Define.h"
#import "UIImageView+WebCache.h"
#import "ShareListBottomButton.h"

@interface UserShareListCell ()

@property (nonatomic, strong) UIImageView *userHeadImage;
@property (nonatomic, strong) UILabel *usernameLabel;
@property (nonatomic, strong) UILabel *shareTitle;
@property (nonatomic, strong) UILabel *shareContent;
@property (nonatomic, strong) UIImageView *shareImage;
@property (nonatomic, strong) UIView *grayLineLabel;

@property (nonatomic, strong) ShareListBottomButton *praiseButton;
@property (nonatomic, strong) ShareListBottomButton *commentButton;
@property (nonatomic, strong) UIView *bottomGrayView;
@property (nonatomic, strong) UILabel *publishTime;


@end

@implementation UserShareListCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
//        float scaleX = kScreenWidth / 375;
//        float scaleY = kScreenHeight / 667;
        
        // 用户头像
        self.userHeadImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 35, 35)];
        self.userHeadImage.layer.cornerRadius = 35 / 2;
        self.userHeadImage.layer.masksToBounds = YES;
        [self.contentView addSubview:_userHeadImage];
        
        // 用户名字
        self.usernameLabel = [[UILabel alloc]initWithFrame:CGRectMake(self.userHeadImage.width + self.userHeadImage.x, self.userHeadImage.y, kScreenWidth - 110, 35)];
        self.usernameLabel.text = @"名字";
        [self.contentView addSubview:_usernameLabel];
        
        // 发表时间;
        self.publishTime = [[UILabel alloc]initWithFrame:CGRectMake(kScreenWidth - 80, self.usernameLabel.y, 50, self.usernameLabel.height)];
        self.publishTime.font = [UIFont systemFontOfSize:10];
        [self addSubview:_publishTime];
        
        // 分享标题
        self.shareTitle = [[UILabel alloc]initWithFrame:CGRectMake(30, self.usernameLabel.y + self.usernameLabel.height, kScreenWidth - 60, 40)];
        self.shareTitle.text = @"分享标题";
        self.shareTitle.font = [UIFont systemFontOfSize:18 weight:1];
        [self.contentView addSubview:_shareTitle];
        
        // 分享内容
        self.shareContent = [[UILabel alloc]initWithFrame:CGRectMake(30, self.shareTitle.y + self.shareTitle.height, kScreenWidth - 60, 60)];
        self.shareContent.text = @"分享内容";
        self.shareContent.font = [UIFont systemFontOfSize:15 weight:1];
        self.shareContent.numberOfLines = 0;
        [self.contentView addSubview:_shareContent];
        
        // 分享的图片
        self.shareImage = [[UIImageView alloc]initWithFrame:CGRectMake(30, self.shareContent.y + self.shareContent.height, 80, 80)];
        [self.contentView addSubview:_shareImage];
        
//        self.usernameLabel.backgroundColor = [UIColor redColor];
//        self.userHeadImage.backgroundColor = [UIColor redColor];
//        self.shareImage.backgroundColor = [UIColor redColor];
//        self.shareContent.backgroundColor = [UIColor redColor];
//        self.commentNumber.backgroundColor = [UIColor redColor];
        
        // 评论人数上面的分割线
        self.grayLineLabel = [[UIView alloc]initWithFrame:CGRectMake(self.shareImage.x, self.shareImage.y + 2, 150 + kScaleX, 2)];
        self.grayLineLabel.layer.cornerRadius = 2;
        self.grayLineLabel.backgroundColor = [UIColor grayColor];
        [self.contentView addSubview:_grayLineLabel];
        
        // 评论的Button;
        self.commentButton = [[ShareListBottomButton alloc]initWithFrame:CGRectMake(30, 0, 50, 15)];
        
        [self.contentView addSubview:_commentButton];

        
        self.bottomGrayView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, 15)];
        self.bottomGrayView.backgroundColor = [UIColor colorWithRed:235 / 255.0 green:236 / 255.0 blue:238 / 255.0 alpha:1];
        [self.contentView addSubview:_bottomGrayView];
    }
    return self;
}

- (void)setModel:(UserShareModel *)model
{
    [self.userHeadImage sd_setImageWithURL:[NSURL URLWithString:model.userInfoModel.userHeadImage] placeholderImage:[UIImage imageNamed:@"iconfont-iconfontyonghutouxiang"]];
    self.usernameLabel.text = model.userInfoModel.username;
    self.shareTitle.text = model.shareTitle;
    self.shareContent.text = model.shareContent;
    self.publishTime.text = [model.publishTime substringWithRange:NSMakeRange(5, 12)];
    NSLog(@"%@",self.shareContent.text);
    //    改变frame
    [self.shareImage sd_setImageWithURL:[NSURL URLWithString:model.shareImage] placeholderImage:nil];
    CGSize shareContentSize = [self stringSizeWithFont:self.shareContent.font string:self.shareContent.text width:kScreenWidth];
    
    if (shareContentSize.height > 80)
    {
        shareContentSize.height = 80;
    }
    
    if (model.shareImage.length == 0)
    {
        self.shareImage.frame = CGRectMake(0, 0, 0, 0);
        self.shareContent.height = shareContentSize.height;
        self.grayLineLabel.y = self.shareContent.y + self.shareContent.height + 5;
        self.commentButton.y = self.grayLineLabel.y + 8;
        self.bottomGrayView.y = self.commentButton.y + self.commentButton.height + 20;
    }
    else
    {
        self.shareContent.height = shareContentSize.height;
        self.shareImage.frame = CGRectMake(30, self.shareContent.y + self.shareContent.height, 80, 80);
        self.grayLineLabel.y = self.shareImage.y + self.shareImage.height + 5;
        self.commentButton.y = self.grayLineLabel.y + 8;        
        self.bottomGrayView.y = self.commentButton.y + self.commentButton.height + 20;
    }
}

- (CGSize)stringSizeWithFont:(UIFont *)font string:(NSString *)string width:(CGFloat)width
{
    CGRect rect = [string boundingRectWithSize:CGSizeMake(width, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:font} context:nil];
    return rect.size;
}

















@end
